/**
 * Copyright (c) 2012 Nokia Corporation.
 */

package com.nokia.example.productcatalog;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

/**
 * The main class of the application. For the In-application Purchase
 * implementation see ProductCatalogForm.java.
 */
public class Main
	extends MIDlet
	implements CommandListener
{
	// Members
    private ProductCatalogForm productCatalogForm = null;
    private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private final String[] productIds = new String[] { "588547", "588549", "588550", "588551" };
    private static Boolean nonTouch = null;
    
    /**
     * From MIDlet.
     */
    public void startApp() {
        try {
            com.nokia.mid.ui.VirtualKeyboard.hideOpenKeypadCommand(true);
        }
        catch (Throwable e) {}
        
        productCatalogForm = new ProductCatalogForm(this, "Product Catalog", productIds);
        productCatalogForm.addCommand(exitCommand);
        productCatalogForm.setCommandListener(this);
        Display.getDisplay(this).setCurrent(productCatalogForm);
        productCatalogForm.init();
    }
    
    /**
     * From MIDlet.
     */
    public void pauseApp() {
    }
    
    /**
     * From MIDlet.
     */
    public void destroyApp(boolean unconditional) {
        exitMIDlet();
    }

    /**
     * From CommandListener.
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == exitCommand) {
            exitMIDlet();
        }
    }
    
    /**
     * Exits the application.
     */
    public void exitMIDlet(){
        notifyDestroyed();
    }
    
    /** 
     * @return True if the device is non-touch, false otherwise.
     */
    public static boolean isNonTouch() {
        if (nonTouch == null) {
            Canvas canvas = new Canvas() {
                protected void paint(Graphics g) {
                }
            };
            
            nonTouch = !canvas.hasPointerEvents() ? Boolean.TRUE : Boolean.FALSE;
        }
        
        return nonTouch.booleanValue();
    }
}
